


#include <stdint.h>
#include <stdio.h>
#include <string.h>

#include "file_util.h"

#include "gui_cjson_api.h"
#include "gui_logist.h"
#include  "plcgw_cfg.h"
#include "string_tools.h"
#include "print_log.h"
#include "read_local_file.h"


//C:\GUI\LVGL\che\tina-F133\mnt\UDISK\gw_plc\data\cfg_dat



//gw_commu_t   gw_commu_obj;

static gw_commu_t gw_commu_info;


/***
PA03, input，opto2
****/
static void print_gpio_def()
{
	int i;
	int port;
	int pin;
	//char buf[32];
	char pstr_in[]="input";
	char pstr_out[]="ouput";
	const gpio_def_t *p_gpio = gw_commu_info.gpio_tab;
	for(i=0; i<MAX_GPIO_NUM; i++) {
		if(p_gpio->name[0]) {
			pin = p_gpio->gpio&0x1f;
			port = (p_gpio->gpio>>5) + 'A';
			const char *pstr;
			if(p_gpio->direct) {
				pstr = pstr_out;
			}
			else {
				pstr = pstr_in;
			}
			printf("P%c%x, %s, %s, onLevel=%d", port, pin, pstr, p_gpio->name,p_gpio->on_level);
			
		}
		p_gpio++;
	}

}




static void printf_ini(void)
{
	printf("plc_uart=%s\n", gw_commu_info.plc_uart);
	printf("setup_uart=%s\n", gw_commu_info.setup_uart);
	

	
	printf("main_platform_ip=%s\n", gw_commu_info.platform_cfg[0].ip);//-------------------平台服务器--ip------
	printf("main_platform_port=%d\n", gw_commu_info.platform_cfg[0].port);
	printf("main_platform_protocol=%d\n", gw_commu_info.platform_cfg[0].protocol);
	printf("main_platform_heartbeastTime=%d\n", gw_commu_info.platform_cfg[0].heartbeast_time);
	
	printf("north1_ip=%s\n", gw_commu_info.platform_cfg[1].ip);
	printf("north1_port=%d\n", gw_commu_info.platform_cfg[1].port);
	printf("north1_protocol=%d\n", gw_commu_info.platform_cfg[1].protocol);
	printf("north1_heartbeastTime=%d\n", gw_commu_info.platform_cfg[1].heartbeast_time);
	
	printf("north2_ip=%s\n", gw_commu_info.platform_cfg[2].ip);
	printf("north2_port=%d\n", gw_commu_info.platform_cfg[2].port);
	printf("north2_protocol=%d\n", gw_commu_info.platform_cfg[2].protocol);
	printf("north2_heartbeastTime=%d\n", gw_commu_info.platform_cfg[2].heartbeast_time);
		

	printf("north3_ip=%s\n", gw_commu_info.platform_cfg[3].ip);
	printf("north3_port=%d\n", gw_commu_info.platform_cfg[3].port);
	printf("north3_protocol=%d\n", gw_commu_info.platform_cfg[3].protocol);
	printf("north3_heartbeastTime=%d\n", gw_commu_info.platform_cfg[3].heartbeast_time);


	printf("manager_plaform_ip=%s\n", gw_commu_info.platform_cfg[4].ip);
	printf("manager_platform_port=%d\n", gw_commu_info.platform_cfg[4].port);
	printf("manager_platform_protocol=%d\n", gw_commu_info.platform_cfg[4].protocol);
	printf("manager_platform_heartbeastTime=%d\n", gw_commu_info.platform_cfg[4].heartbeast_time);


	printf("log_screen=%d\n", gw_commu_info.log_screen);
	printf("node_report_interval=%d\n", gw_commu_info.node_workdata_report_interval);

	print_gpio_def();
	
	
}



//整型ip转换为字符串ip
void PortToString(const uint16_t port,char* str_port )
{		
	memset(str_port,0,6);
	uitoa(port,10,(char*)str_port); 
	
}	
		
	
//字符串ip 转换为  整型ip
void PortToUint(const char* str_port , uint16_t* port)
{		
	*port =  atoi16((char*)str_port,10);		
			
}



//--------------获取服务器--ip------
//输入参数 ch服务器通道=== enum gw_ini_bin_server_index
char*  rlf_read_gw_ip( uint8_t ch  )
{ 
	if(ch>=GW_INI_CH_MAX)
	{
		printf("read_gw_ip ch=%d error\n",ch);		
		return NULL;
	}
   return gw_commu_info.platform_cfg[ch].ip ;  
}

uint16_t  rlf_read_gw_port( uint8_t ch  )
{ 
	if( ch>=GW_INI_CH_MAX )
	{
		printf("read_gw_port ch=%d error\n",ch);
		return 0;
	}	

   return gw_commu_info.platform_cfg[ch].port ;  
}



//--------------获取平台服务器--ip------
void rlf_get_gw_main_platform_ip_port( char* ip_str,char* port_str )
{ 
   strcpy(ip_str,gw_commu_info.platform_cfg[0].ip );  
   PortToString( gw_commu_info.platform_cfg[0].port , port_str );

   json_mainIpRead( );
   
}

//--------------更改平台服务器--ip------
void rlf_set_gw_main_platform_ip_port( const char* ip_str,const char* port_str )
{
   memset(gw_commu_info.platform_cfg[0].ip ,0,sizeof(gw_commu_info.platform_cfg[0].ip) );
   strcpy( gw_commu_info.platform_cfg[0].ip , ip_str);  
   PortToUint(  port_str ,&gw_commu_info.platform_cfg[0].port);

   //------设置写保存标记---------------
////......??????
   json_write_mainIpSet(ip_str , gw_commu_info.platform_cfg[0].port );

}





void read_local_data_cfg_dat(void )
{

FILE *fp=NULL;

   memset((char*) &gw_commu_info,0,sizeof(gw_commu_t) );


    fp = fopen(nv_gw_ip_bin_file_path,"rb");  

    if(fp != NULL) {

        printf("open file %s ok \n", nv_gw_ip_bin_file_path );

      // read_total_file(fp, &gui_gw_param.nv_gw_msg, sizeof(nv_gw_msg_t));
      // read_size_file(fp,0, &gui_gw_param.nv_gw_msg, sizeof(nv_gw_msg_t));

      //  read_size_file(fp,0, buf, sizeof(nv_gw_msg_t) ) ;

    //  read_size_file(fp,0, nv_gw_msg_obj.Buffer, sizeof(nv_gw_msg_u) ) ;
    //   PrintHex( nv_gw_msg_obj.Buffer,sizeof(nv_gw_msg_u)  ); 
	
       read_size_file(fp,0, (uint8_t*) &gw_commu_info, sizeof(gw_commu_t) );
       PrintHex( (uint8_t*)&gw_commu_info,sizeof(gw_commu_t) ); 


       fclose(fp);

    }else{
        printf("open file %s fail \n", nv_gw_ip_bin_file_path );

    }


#if  1
   printf("server ip=%s\n",   gw_commu_info.platform_cfg->ip );
   printf("protocol=%c\n",    gw_commu_info.platform_cfg->protocol );
   printf("link_way=%c\n",    gw_commu_info.platform_cfg->link_way );
   printf("port=%d\n",        gw_commu_info.platform_cfg->port);

#endif  

	//read_total_file(fp, &gui_gw_param.nv_gw_msg, sizeof(nv_gw_msg_t));

   printf_ini( );


}



#if   0

/***
check  gateway.ini and gw_ini.bin
replace the old one by the new one

****/
const gw_commu_t *read_gateway_ini()
{
	FILE *fp;
	char file_name[64];
	char file_bin[64];

	struct stat statbuf={0};
	time_t time_ini;
	time_t time_bin;
		
	snprintf(file_name, sizeof(file_name), "%s%s%s", APP_PATH, CFG_DIR, GATEWAY_INI_FILE);
	int ret = stat(file_name,&statbuf);
	time_ini = statbuf.st_mtime;
	printf("ini mtime=%s\n", ctime(&statbuf.st_mtime));
	

	statbuf.st_mtime = 0;
	snprintf(file_bin, sizeof(file_bin), "%s%s%s", APP_PATH, CFG_DIR, GATEWAY_INI_BIN);
	ret = stat(file_bin,&statbuf);
	time_bin = statbuf.st_mtime;
	printf("bin mtime=%s\n", ctime(&statbuf.st_mtime));
	

	if(time_ini > time_bin) {
		printf(".ini file is new\n");
		fp = fopen(file_name,"r");
		if (fp != NULL) {
			char buf[100]={0};
			int cnt =0;
			char *pstr = NULL;
			while((fgets(buf,sizeof(buf),fp))!=NULL)   {
				replace_spec_char(buf, ',', ' ');
			    replace_spec_char(buf, '=', ' ');
				ini_cmd_process(buf, ini_cmd_tab);
			}
			fclose(fp);

			//get_other_gateway_msg();
			//printf_ini();
			fp = fopen(file_bin, "wb");
			if(fp) {
				fwrite(&gw_commu_info, 1, sizeof(gw_commu_info), fp);
				fclose(fp);
				printf("update gw bin now\n");
			}
		}
		else {
			printf("no file %s\n", file_name);
		}

	}
	else {
		fp = fopen(file_bin,"rb");
		if(fp != NULL) {
			memset(&gw_commu_info, 0, sizeof(gw_commu_info));
			read_total_file(fp, &gw_commu_info, sizeof(gw_commu_info));

			fclose(fp);
		}
	
	}

	printf_ini();

	return &gw_commu_info;

}



#endif    










